frames reset
scalar drop _all 
matrix drop _all
mata: mata clear
set more off 

local datadir "${clean_data}"

******************* Set Parameters *********************************************


************ CHOOSE COMPARISON OCCUPATIONS *************************************
	
** Which occupations? 

	local occnums 1 2

/*
Dictionary:
1 "Physicians" 

2 "Lawyers" 
*/

*************** Set starting and ending ages **************************

local minage = 20

local maxage = 70


********************************************************************************
**********************      END USER INPUT    **********************************
********************************************************************************


#delimit ;

local occs 

"
physicians 
lawyers 
"
;

#delimit cr


foreach i in `occnums'{

		local o: word `i' of `occs'
		************** LOADING AND IMPLEMENTING SAMPLE RESTRICTIONS ****************
		local pcpflag = 0
		
		if "`o'" == "pcps"{
			local o "physicians"
			local pcpflag = 1
		}
		use "`datadir'/panel_`o'_clean", clear

		* Must have been between 30-60 at time of ACS 
		* Note that age_survey is missing for "NPPES only" observations
		* note that `o' is plural, but occupation variables are singular
		local o_singular = substr("`o'",1,strlen("`o'") - 1)

		if "`o'" == "physicians"{
			keep if `o_singular'_nppes == 1  
		}
		
		if `pcpflag' == 1 {
			keep if spec_category_code == 1
			local o "pcps"
		}
									
		 
		else {
			
			keep if inrange(age, `minage', `maxage')
			
			} 
		
		keep if year == 2017
		
		*keeping only obs where we observe both hrs worked and income for that year
		/*This part restricts to people in NPPES who are also found in ACS b/c year_survey only
		present in ACS*/
		
		bys personid year: gen flag = 1 if  !mi(wkh) & (year==year_survey)
		keep if flag == 1

		*gen hrs worked per year 
		gen hrswork_yr = wkw_imputed * wkh	
		gen hrswork_yr_adj = wkw_imputed*wkh^1.34
		

		*****************************************
		******* For all physicians/lawyers ******
		*****************************************
		
		preserve
		
		*to get age profiles for annual hours 
		
		gcollapse (mean) hrswork_yr hrswork_yr_adj (count)  N_hrswork_yr = hrswork_yr , ///
		by(age year) 

		gcollapse (rawsum) unscaled_hrs = hrswork_yr scaled_hrs1 = hrswork_yr_adj  N_hrswork_yr  , by(year) 
		
		foreach v in unscaled_hrs  scaled_hrs1  {
				
		drbest_docinc `v'
			
		}
		gen occ = "`o'"		
		
		tempfile npv_hrs_`o'
				
		save `npv_hrs_`o''
		
		restore
		
		
		
		************************************
		******* For PCPs ******
		************************************
		
		
		if "`o'" == "physicians" {
		
		
		drop if pr_taxonomy_code == "390200000X" | mi(pr_taxonomy_code)
		
		
		keep if spec_category_code == 1	
		

				*to get age profiles for annual hours 
		
		gcollapse (mean) hrswork_yr hrswork_yr_adj (count)  N_hrswork_yr = hrswork_yr , ///
		by(age year) 
		gcollapse (rawsum) unscaled_hrs = hrswork_yr scaled_hrs1 = hrswork_yr_adj  N_hrswork_yr , by(year) 
		
		foreach v in unscaled_hrs  scaled_hrs1   {
				
				
		drbest_docinc `v'
												}
		
		gen occ = "pcps"
		
		tempfile npv_hrs_allspec
		
		save `npv_hrs_allspec'
		
		}
		
		
}	

clear
use  `npv_hrs_allspec'
foreach o in physicians lawyers {
	append using `npv_hrs_`o''
}

order occ N* *scaled*
gen N_UR = N_hrswork_yr
gen bucket = "hrswork_yr"
drbest_docinc unscaled_hrs scaled_hrs1 
drbcount N_hrswork_yr, replace
order N_UR, last
export delimited using "${mypath}/intermediate_csv/pdv04-pdv_hours.csv", replace

